import {
	apiBaseUrl
} from './config.js';
import * as common from './common.js' //引入common
import * as db from './db.js' //引入common
// 需要登陆的，都写到这里，否则就是不需要登陆的接口
const methodsToken = ['api/wx/decryptphone'];


const post = (method, data, callback, complete) => {
	uni.removeStorage({
		key: 'jump'
	})
	// 获取用户token
	let headers = {
			'Accept': 'application/json',
			'Content-Type': 'application/x-www-form-urlencoded'
	}
	let userToken = db.get("userToken");
	let openid = db.get("openid");
	if (userToken) {
		headers.Authorization = userToken
	}
	if (openid) {
		headers.openid = openid
	}

	let postData = JSON.parse(JSON.stringify(data))
	delete postData.methodType
	uni.request({
		url: apiBaseUrl + method,
		data: postData,
		header: headers,
		method: data.methodType || 'post',
		success: (response) => {
			const result = response.data
			if (!result.status) {
				// 登录信息过期或者未登录
				if (result.code === 201) {
					db.del("userToken");
					let jump = uni.getStorageSync('jump')
					console.log(jump, 1111)
					if (!jump) {
						uni.setStorageSync('jump', true)
						uni.showToast({
							title: result.message,
							icon: 'none',
							duration: 1500
						});
						setTimeout(() => {

							uni.navigateTo({
								url: '/pages/login/index'
							})
						}, 1500)
					}
				}
			}
			callback(result);
		},

		complete: (response) => {
			setTimeout(function() {
				uni.hideLoading();
			}, 1000)
			complete ? complete() : "";
		},
		fail: (error) => {
			uni.showLoading({
				title: '网络开小差了'
			});
			// setTimeout(function() {
			uni.hideLoading();
			// }, 1000)
			if (error && error.response) {
				showError(error.response);
			} else {

			}

		},
	});

}
const get = (url, callback) => {
	uni.showLoading({
		title: '加载中'
	});
	uni.request({
		url: url,
		header: {
			'Accept': 'application/json',
			'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
		},
		method: 'GET',
		success: (response) => {
			uni.hideLoading();
			callback(response.data);
		},
		fail: (error) => {
			uni.hideLoading();
			if (error && error.response) {
				showError(error.response);
			}
		},
		complete: () => {
			setTimeout(function() {
				uni.hideLoading();
			}, 250);
		}
	});
}

const showError = error => {
	let errorMsg = ''
	switch (error.status) {
		case 400:
			errorMsg = '请求参数错误'
			break
		case 401:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = `请求地址出错: ${error.config.url}`
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = error.msg
			break
	}

	uni.showToast({
		title: errorMsg,
		icon: 'none',
		duration: 1000,
		complete: function() {
			setTimeout(function() {
				uni.hideToast();
			}, 1000);
		}
	});
}

// 图片上传

export const uploadImage = (count,callback) => {
	uni.chooseImage({
		count:count,
		success: (chooseImageRes) => {
			uni.showLoading({
				title: '上传中...'
			});
			const tempFilePaths = chooseImageRes.tempFilePaths;
			console.log(tempFilePaths,11111)
			
			tempFilePaths.some(item=>{
				
				const uploadTask = uni.uploadFile({
					url: apiBaseUrl + 'api/file/upload', //仅为示例，非真实的接口地址
					filePath:item,
					fileType: 'image',
					name: 'file',
					header: {
				
						// 'Accept': 'application/json',
						// 'Content-Type': 'application/x-www-form-urlencoded',
						'Authorization': db.get("userToken")
					},
					formData: {
						'file': tempFilePaths[0],
				
					},
					success: (uploadFileRes) => {
						callback(JSON.parse(uploadFileRes.data));
					},
					fail: (error) => {
						if (error && error.response) {
							showError(error.response);
						}
					},
					complete: () => {
						setTimeout(function() {
							uni.hideLoading();
						}, 250);
					}
				});
			})
		}
	});
}


// 图片上传
export const uploadVideo = (callback) => {
	uni.chooseVideo({
		success: (chooseImageRes) => {
			uni.showLoading({
				title: '上传中...'
			});
			console.log(chooseImageRes,12212)
			const tempFilePaths = chooseImageRes.tempFilePath;
			const uploadTask = uni.uploadFile({
				url: apiBaseUrl + 'api/file/upload', //仅为示例，非真实的接口地址
				filePath: tempFilePaths,
				name: 'file',
				header: {

					// 'Accept': 'application/json',
					// 'Content-Type': 'application/x-www-form-urlencoded',
					'Authorization': db.get("userToken")
				},
				formData: {
					'file': tempFilePaths,

				},
				success: (uploadFileRes) => {
					callback(JSON.parse(uploadFileRes.data));
				},
				fail: (error) => {
					if (error && error.response) {
						showError(error.response);
					}
				},
				complete: (e) => {
					console.log(e,21212)
					setTimeout(function() {
						uni.hideLoading();
					}, 250);
				}
			});
		}
	});
}

// 文件上传
export const uploadFiles = (callback) => {
	uni.chooseImage({
		count:1,
		success: (chooseImageRes) => {
			uni.showLoading({
				title: '上传中...'
			});
			const tempFilePaths = chooseImageRes.tempFilePaths;
			const uploadTask = uni.uploadFile({
				url: apiBaseUrl + 'api/file/upload', //仅为示例，非真实的接口地址
				filePath: tempFilePaths[0],
				fileType: 'image',
				name: 'file',
				header: {

					// 'Accept': 'application/json',
					// 'Content-Type': 'application/x-www-form-urlencoded',
					'Authorization': db.get("userToken")
				},
				formData: {
					'file': tempFilePaths[0],

				},
				success: (uploadFileRes) => {
					callback(JSON.parse(uploadFileRes.data));
				},
				fail: (error) => {
					if (error && error.response) {
						showError(error.response);
					}
				},
				complete: () => {
					setTimeout(function() {
						uni.hideLoading();
					}, 250);
				}
			});
		}
	});
}

// 登录
export const wxLogin = (data, callback) => post('api/wx/login', data, callback);
export const memberInfo = (data, callback) => post('api/member/info', data, callback);

export const verifyRegister = (data, callback) => post('api/verify/register', data, callback);


export const vehicleList = (data, callback) => post('api/member/vehicle_list', data, callback);
export const vehicleSave = (data, callback) => post('api/member/save_vehicle', data, callback);
export const vehicleEdit = (data, callback) => post('api/member/update_vehicle', data, callback);
export const vehicleInfo = (data, callback) => post('api/member/vehicle_detail', data, callback);
export const myAppointInfo = (data, callback) => post('api/member/appoint_detai', data, callback);

export const vehicleDel = (data, callback) => post('api/member/delete_vehicle', data, callback);
export const appointList = (data, callback) => post('api/appoint_active/list', data, callback);
export const appointInfo = (data, callback) => post('api/appoint_active/detail', data, callback);
export const appointSave = (data, callback) => post('api/member/save_appoint', data, callback);
export const appointCancel = (data, callback) => post('api/member/cancel_appoint', data, callback);
export const appointSign = (data, callback) => post('api/member/sign_appoint', data, callback);
export const myAppointList = (data, callback) => post('api/member/appoint_list', data, callback);
export const adList = (data, callback) => post('api/ad/list', data, callback);
export const articleList = (data, callback) => post('api/article/list', data, callback);
export const articleInfo = (data, callback) => post('api/article/detail', data, callback);
export const dictList = (data, callback) => post('api/dict-data/list', data, callback);
export const questionInfo = (data, callback) => post('api/question/info', data, callback);
export const questionTopic= (data, callback) => post('api/question/topic', data, callback);
export const userUpdate= (data, callback) => post('api/member/update', data, callback);
export const submit_question= (data, callback) => post('api/member/submit_question', data, callback);

// 微信获取手机号码
export const wxPhone = (data, callback) => post('api/wx/decryptphone', data, callback);

export const userUpdateMobile = (data, callback) => post('api/member/update_mobile', data, callback);

export const appoint_detail = (data, callback) => post('api/member/appoint_detail', data, callback);
export const supplierDetail = (data, callback) => post('api/supplier/detail', data, callback);
export const supplierList = (data, callback) => post('api/supplier/list', data, callback);
export const supplierActive = (data, callback) => post('api/supplier/active', data, callback);
export const supplierCity = (data, callback) => post('api/supplier/city', data, callback);

export const depositList = (data, callback) => post('api/deposit-management/list', data, callback);


// 房源模块
export const getHousingList = (data, callback) => post('api/housing/product/list', data, callback);

export const getHousingType = (data, callback) => post('api/housing/product/getHousingType', data, callback);

export const getHousingDetail = (data, callback) => post('api/housing/product/detail', data, callback);

export const cluemanagementDetail = (data, callback) => post('api/crm/cluemanagement/detail', data, callback);


export const cluemanagementUpdate = (data, callback) => post('api/crm/cluemanagement/update', data, callback);

export const cluemanagementSave = (data, callback) => post('api/crm/cluemanagement/save', data, callback);
export const cluemanagementDel = (data, callback) => post('api/crm/cluemanagement/delete', data, callback);
export const collectAdd = (data, callback) => post('api/member/house-collect/create', data, callback);
export const collectCancel= (data, callback) => post('api/member/house-collect/cancel', data, callback);

// 登录
export const verifyLogin = (data, callback) => post('api/verify/login', data, callback);
export const verifySendCode = (data, callback) => post('api/verify/sendCode', data, callback);

// 房源相关
export const notifyGetCategoryList = (data, callback) => post('api/crm/notify/getCategoryList', data, callback);

// 申请
export const applyForBroker = (data, callback) => post('api/member/applyForBroker', data, callback);

export const applyForLandlord = (data, callback) => post('api/member/applyForLandlord', data, callback);
export const getSubmit = (data, callback) => post('api/member/getSubmit', data, callback);

// 地址管理
export const getAreaList = (data, callback) => post('api/area/tree', data, callback);

// 个人中心
export const getPerFormance = (data, callback) => post('api/member/getPerFormance', data, callback);
export const getMeHousing = (data, callback) => post('api/crm/cluemanagement/list', data, callback);
export const getChannelHead = (data, callback) => post('api/member/getChannelHead', data, callback);
export const getMyClient = (data, callback) => post('api/member/getMyClient', data, callback);
export const getMyCollect = (data, callback) => post('api/member/house-collect/list', data, callback);
export const withdrawDeposit = (data, callback) => post('api/pay/wallet/withdrawDeposit', data, callback);

// 银行卡
export const bankList = (data, callback) => post('api/member/getBank', data, callback);
export const bankAdd = (data, callback) => post('api/member/addBank', data, callback);
export const bankEdit = (data, callback) => post('api/member/updateBank', data, callback);
export const bankDel = (data, callback) => post('api/member/deleteBank', data, callback);
export const getMeWallet = (data, callback) => post('api/pay/wallet/getMeWallet', data, callback);
export const buyProduct = (data, callback) => post('api/investment/product/buyProduct', data, callback);
export const walletList = (data, callback) => post('api/pay/wallet/my/list', data, callback);


// 消息
export const noticeList = (data, callback) => post('api/notice', data, callback);
export const noticeInfo = (data, callback) => post('api/notice/detail', data, callback);
export const getNumber = (data, callback) => post('api/investment/product/getNumber', data, callback);
export const getAllInvestmentProduct = (data, callback) => post('api/investment/product/getAllInvestmentProduct', data, callback);
export const msgList = (data, callback) => post('api/member/msg/list', data, callback);

export const signApply = (data, callback) => post('api/sign/apply', data, callback);
export const file_finish = (data, callback) => post('api/sign/file_finish', data, callback);
export const buyList = (data, callback) => post('api/housing/product/buy-list', data, callback);
export const productMy = (data, callback) => post('api/housing/product/my/list', data, callback);
export const incomeDistrbutionList = (data, callback) => post('api/income-distrbution-item/list', data, callback);

export const xsTotal = (data, callback) => post('api/income/xs/total', data, callback);
export const xsList = (data, callback) => post('api/income/xs/list', data, callback);
export const ywTotal = (data, callback) => post('api/income/yw/total', data, callback);
export const ywList = (data, callback) => post('api/income/yw/list', data, callback);

export const yjTotal = (data, callback) => post('api/income/yj/total', data, callback);
export const yjList = (data, callback) => post('api/income/yj/list', data, callback);

export const zjTotal = (data, callback) => post('api/income/zj/total', data, callback);
export const zjList = (data, callback) => post('api/income/zj/list', data, callback);

export const productCheck = (data, callback) => post('api/investment/product/check', data, callback);
export const incomeList = (data, callback) => post('api/income-distrbution/list', data, callback);
